//
//  RootViewController.m
//  豆瓣
//
//  Created by hy on 15/12/31.
//  Copyright © 2015年 hongyu. All rights reserved.
//

#import "RootViewController.h"
#import "HdTableViewCell.h"
#import "HdModel.h"
#import "UIImageView+WebCache.h"
#import "erViewController.h"
#import "hydateBaseHandle.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)NSMutableDictionary *result;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *modelArray;
@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_modelArray release];
    [_result release];
    [_array release];
    [_table release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"活动";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, -5, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    _table.rowHeight = 100;
    _table.separatorColor = [UIColor whiteColor];
    [self.view addSubview:_table];
    [_table release];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[HdTableViewCell class] forCellReuseIdentifier:@"cell"];
//    JSON解析
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    self.result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.array = _result[@"events"];
    self.modelArray = [NSMutableArray array];
    for (NSDictionary *dic in _array) {
        HdModel *model = [HdModel hdmodel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArray addObject:model];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.lanImage.image = [UIImage imageNamed:@"bg_eventlistcell.png"];
    cell.baiImage.image = [UIImage imageNamed:@"bg_share_large"];
    cell.a.image = [UIImage imageNamed:@"icon_date@2x.png"];
    cell.b.image = [UIImage imageNamed:@"dingwei.png"];
    cell.c.image = [UIImage imageNamed:@"icon_catalog@2x"];
    
//    创建毛豆
    HdModel *model = _modelArray[indexPath.row];
    cell.titleLabel.text = model.title;
    NSString *str1 = [model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str2 = [model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str3 = [NSString stringWithFormat:@"%@ -- %@", str1, str2];
    cell.timeLabel.text = str3;
    cell.addressLabel.text = model.address;
    cell.leixing.text = [NSString stringWithFormat:@"类型 : %@", model.category_name];
    cell.ziLable.text = @"感兴趣:           参加:";
    cell.shuzuLable.text = [NSString stringWithFormat:@"%@             %@", model.participant_count, model.wisher_count];
    [cell.sb sd_setImageWithURL:[NSURL URLWithString:model.image]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 190;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    erViewController *vc = [[erViewController alloc] init];
    vc.dic = _array[indexPath.row];
//    跳过去隐藏tabbar
    vc.hidesBottomBarWhenPushed = YES;
    NSLog(@"%@", vc.dic);
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
